<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="./css/style.css">
    <title>选择收货地址</title>
</head>
<script type="text/javascript">
    window.onload=function () {
            document.addEventListener('touchstart',function (event) {
                if(event.touches.length>1){
                    event.preventDefault();
                }
            })
            var lastTouchEnd=0;
            document.addEventListener('touchend',function (event) {
                var now=(new Date()).getTime();
                if(now-lastTouchEnd<=300){
                    event.preventDefault();
                }
                lastTouchEnd=now;
            },false)
        }
</script>
<body>
<div id="personal_center">
    <div class="t-my-attr-container">
        <header class="cl" v-show="header.show">
            <div class="nav-bar cl">
                <span class="goback arrow_l" @click="goback"></span>
                <span class="page-name" v-cloak>{{shop_name}}会员店</span>
            </div>
        </header>
        <div class="t-my-att-main">
            <div class="t-my-att-content t-address-content t-select-address-content">
                <ul>   
                    <li v-for="(item,$index) in items">
                        <a :href="selectedaddr(item.addr_id)" >
                            <div class="t-box-flex">
                                <div class="icons-gou">
                                    <i :class="{active:$index===selected}"><em ></em></i>
                                </div>
                                <p>
                                   <span  :class="{pActive:$index===pName}" v-cloak>{{item.recevier}}</span>
                                   <span :class="{pActive:$index===pTel}"  v-cloak>{{item.mobile}}</span>
                                   <span class="add-default" v-if="item.def_addr == 1">默认</span>
                                </p>
                                <strong v-cloak>{{item.addr_str}}{{item.addr}}</strong>
                            </div>
                        </a>
                        <div class="t-adderss-edit" @click="editAddr(item.addr_id)">
                            <i></i>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
        <span class="loadmore" v-if="warning">待选地址列表为空，请先添加新地址！</span>
    </div>
    <div class="t-footer return-footer add-footer">
        <div class="t-btn-group">
            <a :href="add_addr" class="form-btn form-btn-a"><span class="icon-add">+</span><span>新增地址</span></a>
        </div>
   </div>
</div>
    <script src="./js/lib/vue.js"></script>
    <script src="http://static.jdhui.com/lib/vue-resource/1.2/vue-resource.js"></script>
    <script src="./js/min/config.min.js"></script>
    <script src="./js/min/common.min.js"></script>
    <script src="./js/min/component.min.js"></script>
<script>
var store_id=parseInt(APP.getQueryString("storeid"));
var cartid=APP.getQueryString("cartIds");
var bn_cartid=APP.getQueryString("bncartid");
var ncartidname="";
  if (bn_cartid) {
        n_cartid=bn_cartid;
        ncartidname='bncartid';
    }else{
        n_cartid=cartid;
        ncartidname='cartIds';
    }
    var app = new Vue({
        el:'#personal_center',
        data:{
			header:{
				show: !APP.iswechat()
			},
            items:[],
            selected:'0',
            pName:'0',
            pTel:'0',
            store_id:APP.getQueryString('storeid'),
            cart_id:APP.getQueryString('cartIds'),
            address_id:APP.getQueryString('addressId'),
            coupon_id:APP.getQueryString('couponIds'),
            coupon_amount:APP.getQueryString('amount'),
            shop_name: '',
            warning:false
        },
        mounted:function(){
        	APP.init(this, function ()  {
                    this.shop_name = APP.getCookie('shopname');
                }.bind(this), function (arg)  {
                    this.shop_name = arg.body.data.shop.shop_name;
                }.bind(this));
            this.getData();
            console.log(this.store_id);
            console.log(this.cart_id);
            console.log(this.address_id);
        },
        methods:{
            goback:function(){
                window.history.go(-1);
            },
            getData:function(){
                Vue.http.options.xhr = {
                    withCredentials: true
                }
                Vue.http.options.credentials = true;
                Vue.http.get(APIPHP + 'index.php?r=member/u/recipient&storeid='+ this.store_id).then(function(_res){
                    console.log(_res.body.data.data)
                    this.items = _res.body.data.data;
                    if (this.items.length>0) {
                        _res.body.data.data.forEach(function (v,k) {
                        if(this.address_id){
                            if(this.address_id == v.addr_id){
                                this.selected = k;
                                this.pName = k;
                                this.pTel = k; 
                                console.log('地址id存在'); 
                            }
                        }else{
                           if(v.def_addr == 1){
                                this.selected = k;
                                this.pName = k;
                                this.pTel = k;
                                console.log('默认地址'); 
                            } 
                        }
                        
                    }.bind(this));
                    }else{
                        this.warning=true;
                    }
                    
                }.bind(this))

                },
                selectedaddr : function(addrid){
                return 'checkout.html?storeid='+ store_id + '&'+ncartidname+'=' + n_cartid + '&addressId=' + addrid + '&couponIds=' + this.coupon_id + '&amount=' + this.coupon_amount
            },
            //编辑地址
              editAddr:function(addrId){
                    window.location.href = 'add-address.html?storeid='+ store_id + '&'+ncartidname+'=' + n_cartid+'&addr_id='+addrId;
                }

        },
        computed: {
            add_addr: function(){
                return 'add-address.html?storeid='+store_id + '&'+ncartidname+'=' + n_cartid
            }
        }
    })
</script>
</body>
</html>